<?php
/**
 * 
 * Entity Agent for storig and managing agent operations.
 * @author udit
 *
 */
class Reale_Model_Users_Agent
{
    /**
     * 
     * Stores agent's identification number
     * @var int
     */
    protected $_agent_id;
    /**
     * 
     * Stores agent's name
     * @var string
     */
    protected $_agent_name;
    /**
     * 
     * Stores agent's father's name
     * @var string
     */
    protected $_father_name;
    /**
     * 
     * Stores agent's husband's name (if applicable)
     * @var string
     */
    protected $_husband_name;
    /**
     * 
     * Stores agent's mother's name
     * @var string
     */
    protected $_mother_name;
    /**
     * 
     * Stores agent's mobile number
     * @var string
     */
    protected $_mobile_no;
    /**
     * 
     * Stores agent's alternate number
     * @var string
     */
    protected $_alternate_no;
    /**
     * 
     * Stores agent's rgrn date
     * @var string
     */
    protected $_rgrn_date;
    /**
     * 
     * Stores agent's date of birth
     * @var string
     */
    protected $_dob;
    /**
     * 
     * Stores agent's gender 
     * M => Male
     * F => Female
     * @var string|char
     */
    protected $_gender;
    /**
     * 
     * Stores agent's nationality id
     * IND => Indian 
     * etc...
     * @var string
     */
    protected $_nationality;
    /**
     * 
     * Stores agent's marital status
     * S => Single
     * M => Married
     * D => Divorsed
     * SP => Seperated
     * @var string|char
     */
    protected $_marital_status;
    /**
     * 
     * Stores agent's type
     * IDVL => Indiviudual
     * COMP => Company
     * PART => Partnership
     * PROP => Proprietorship
     * @var string
     */
    protected $_applicant_type;
    /**
     * 
     * Stores agent's activity status
     * A => Active
     * S => Semi Active
     * N => Not Active
     * @var string|char;  
     */
    protected $_activity_status;
    /**
     * 
     * Stores business level of agent
     * 1 => Business Associate
     * 2 => etc....
     * @var string|int;
     */
    protected $_business_level;
    /**
     * 
     * Stores agent's PAN number
     * @var string 
     */
    protected $_pan;
    /**
     * 
     * Stores agent's email address
     * @var string 
     */
    protected $_email;
    /**
     * 
     * Stores agent's occupation
     * @var string 
     */
    protected $_occupation;
    /**
     * 
     * Stores agent's permanent address
     * @var Reale_Model_Location
     */
    protected $_paddress;
    /**
     * 
     * Stores agent's communication/coresspondance address
     * @var Reale_Model_Location
     */
    protected $_caddress;
    /**
     * 
     * Stores agent's sponser
     * @var string|Reale_Model_Users_Agent
     */
    protected $_sponser;
    /**
     * 
     * Stores agent's nominee
     * @var Reale_Model_Users_Nominee
     */
    protected $_nominee;
    /**
     * 
     * Stores agent's password
     * @var string
     */
    protected $_password;
    /**
     * 
     * Stores agent entity mapper
     * @var Reale_Model_Users_AgentMapper
     * 
     */
    protected $_mapper;
    protected $_valid;
    protected $_cansponser;
    /**
     * Constructor
     * 
     * @param  array|null $options 
     * @return void
     */
    public function __construct (array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    
        if (isset($options['agent_id'])) {
            self::isValid();
        }
    }
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @param  mixed $value 
     * @return void
     */
    public function __set ($name, $value)
    {
        $method = 'set' . ucfirst($name);
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Zend_Exception('Invalid property specified', 
            Zend_Log::DEBUG);
        }
        $this->$method($value);
    }
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @return mixed
     */
    public function __get ($name)
    {
        $method = 'get' . ucfirst($name);
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Zend_Exception('Invalid property specified', 
            Zend_Log::DEBUG);
        }
        return $this->$method();
    }
    /**
     * Set state
     * 
     * @param  array $options 
     * @return Reale_Model_Users_Agent
     */
    public function setOptions (array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    /**
     * 
     * Setter for agent_id 
     * @param int $agent_id
     * @return Reale_Model_Users_Agent
     */
    public function setAgent_id ($agent_id)
    {
        $this->_agent_id = $agent_id;
        return $this;
    }
    /**
     * 
     * Getter for agent_id
     * @return int $_agent_id
     */
    public function getAgent_id ()
    {
        if (isset($this->_agent_id)) {
            return $this->_agent_id;
        }
         //throw new Exception('The objects primary key(i.e. $_agent_id) is not set.',Zend_Log::WARN); 
    }
    /**
     * 
     * Setter for agent_name 
     * @param string $agent_name
     * @return Reale_Model_Users_Agent
     */
    public function setAgent_name ($agent_name)
    {
        $this->_agent_name = $agent_name;
        return $this;
    }
    /**
     * 
     * Getter for agent_name
     * @return string $_agent_name
     */
    public function getAgent_name ()
    {
        return $this->_agent_name;
    }
    /**
     * 
     * Setter for father_name 
     * @param string $father_name
     * @return Reale_Model_Users_Agent
     */
    public function setFather_name ($father_name)
    {
        $this->_father_name = $father_name;
        return $this;
    }
    /**
     * 
     * Getter for father_name
     * @return string $_father_name
     */
    public function getFather_name ()
    {
        return $this->_father_name;
    }
    /**
     * 
     * Setter for husband_name (if applicable) 
     * @param string $husband_name
     * @return Reale_Model_Users_Agent
     */
    public function setHusband_name ($husband_name)
    {
        $this->_husband_name = $husband_name;
        return $this;
    }
    /**
     * 
     * Getter for husband_name (if applicable)
     * @return string $_husband_name
     */
    public function getHusband_name ()
    {
        return $this->_husband_name;
    }
    /**
     * 
     * Setter for mother_name 
     * @param string $mother_name
     * @return Reale_Model_Users_Agent
     */
    public function setMother_name ($mother_name)
    {
        $this->_mother_name = $mother_name;
        return $this;
    }
    /**
     * 
     * Getter for mother_name
     * @return string $_mother_name
     */
    public function getMother_name ()
    {
        return $this->_mother_name;
    }
    /**
     * 
     * Setter for mobile number 
     * @param string $mobile_number
     * @return Reale_Model_Users_Agent
     */
    public function setMobile_no ($mobile_no)
    {
        $this->_mobile_no = $mobile_no;
        return $this;
    }
    /**
     * 
     * Getter for mobile number
     * @return string $_mobile_no
     */
    public function getMobile_no ()
    {
        return $this->_mobile_no;
    }
    /**
     * 
     * Setter for alternate number 
     * @param string $alternate_no
     * @return Reale_Model_Users_Agent
     */
    public function setAlternate_no ($alternate_no)
    {
        $this->_alternate_no = $alternate_no;
        return $this;
    }
    /**
     * 
     * Getter for alternate number
     * @return string $_alternate_no
     */
    public function getAlternate_no ()
    {
        return $this->_alternate_no;
    }
    /**
     * 
     * Setter for registration date 
     * @param string $rgrn_date
     * @return Reale_Model_Users_Agent
     */
    public function setRgrn_date ($rgrn_date)
    {
        $this->_rgrn_date = $rgrn_date;
        return $this;
    }
    /**
     * 
     * Getter for registration date 
     * @return string $_rgrn_date
     */
    public function getRgrn_date ()
    {
        return $this->_rgrn_date;
    }
    /**
     * 
     * Setter for date of birth 
     * @param string $dob
     * @return Reale_Model_Users_Agent
     */
    public function setDob ($dob)
    {
        $this->_dob = $dob;
        return $this;
    }
    /**
     * 
     * Getter for date of birth 
     * @return string $_dob
     */
    public function getDob ()
    {
        return $this->_dob;
    }
    /**
     * 
     * Setter for gender 
     * @param string|char $gender
     * @return Reale_Model_Users_Agent
     */
    public function setGender ($gender)
    {
        $this->_gender = $gender;
        return $this;
    }
    /**
     * 
     * Getter for gender 
     * @return string|char $_gender
     */
    public function getGender ()
    {
        return $this->_gender;
    }
    /**
     * 
     * Setter for nationality 
     * @param string $nationality
     * @return Reale_Model_Users_Agent
     */
    public function setNationality ($nationality)
    {
        $this->_nationality = $nationality;
        return $this;
    }
    /**
     * 
     * Getter for nationality 
     * @return string $_nationality
     */
    public function getNationality ()
    {
        return $this->_nationality;
    }
    /**
     * 
     * Setter for marital_status 
     * @param string|char $marital_status
     * @return Reale_Model_Users_Agent
     */
    public function setMarital_status ($marital_status)
    {
        $this->_marital_status = $marital_status;
        return $this;
    }
    /**
     * 
     * Getter for marital_status 
     * @return string|char $_marital_status
     */
    public function getMarital_status ()
    {
        return $this->_marital_status;
    }
    /**
     * 
     * Setter for applicant_type 
     * @param string $applicant_type
     * @return Reale_Model_Users_applicant
     */
    public function setApplicant_type ($applicant_type)
    {
        $this->_applicant_type = $applicant_type;
        return $this;
    }
    /**
     * 
     * Getter for applicant_type 
     * @return string $_applicant_type
     */
    public function getApplicant_type ()
    {
        return $this->_applicant_type;
    }
    /**
     * 
     * Setter for activity_status 
     * @param string $activity_status
     * @return Reale_Model_Users_Agent
     */
    public function setActivity_status ($activity_status)
    {
        $this->_activity_status = $activity_status;
        return $this;
    }
    /**
     * 
     * Getter for activity_status 
     * @return string $_activity_status
     */
    public function getActivity_status ()
    {
        return $this->_activity_status;
    }
    /**
     * 
     * Setter for business_level 
     * @param string $business_level
     * @return Reale_Model_Users_Agent
     */
    public function setBusiness_level ($business_level)
    {
        $this->_business_level = $business_level;
        return $this;
    }
    /**
     * 
     * Getter for business_level 
     * @return string $_business_level
     */
    public function getBusiness_level ()
    {
        return $this->_business_level;
    }
    /**
     * 
     * Setter for pan 
     * @param string $pan
     * @return Reale_Model_Users_Agent
     */
    public function setPan ($pan)
    {
        $this->_pan = $pan;
        return $this;
    }
    /**
     * 
     * Getter for pan 
     * @return string $_pan
     */
    public function getPan ()
    {
        return $this->_pan;
    }
    /**
     * 
     * Setter for email 
     * @param string $email
     * @return Reale_Model_Users_Agent
     */
    public function setEmail ($email)
    {
        $this->_email = $email;
        return $this;
    }
    /**
     * 
     * Getter for email 
     * @return string $_email
     */
    public function getEmail ()
    {
        return $this->_email;
    }
    /**
     * 
     * Setter for occupation 
     * @param string $occupation
     * @return Reale_Model_Users_Agent
     */
    public function setOccupation ($occupation)
    {
        $this->_occupation = $occupation;
        return $this;
    }
    /**
     * 
     * Getter for occupation 
     * @return string $_occupation
     */
    public function getOccupation ()
    {
        return $this->_occupation;
    }
    /**
     * 
     * Setter for paddress 
     * @param Reale_Model_Location $paddress
     * @return Reale_Model_Users_Agent
     */
    public function setPaddress (Reale_Model_Location $paddress)
    {
        $this->_paddress = $paddress;
        return $this;
    }
    /**
     * 
     * Getter for paddress 
     * @return string|Reale_Model_Location 
     */
    public function getPaddress ()
    {
        return $this->_paddress;
    }
    /**
     * 
     * Setter for Caddress 
     * @param Reale_Model_Location $caddress
     * @return Reale_Model_Users_Agent
     */
    public function setCaddress (Reale_Model_Location $caddress)
    {
        $this->_caddress = $caddress;
        return $this;
    }
    /**
     * 
     * Getter for caddress 
     * @return string|Reale_Model_Location 
     */
    public function getCaddress ()
    {
        return $this->_caddress;
    }
    /**
     * 
     * Setter for sponser 
     * @param string|Reale_Model_Users_Agent $sponser
     * @return Reale_Model_Users_Agent
     */
    public function setSponser (Reale_Model_Users_Agent $sponser)
    {
        $this->_sponser = $sponser;
        return $this;
    }
    /**
     * 
     * Getter for sponser 
     * @return string|Reale_Model_Users_Agent 
     */
    public function getSponser ()
    {
        return $this->_sponser;
    }

    /**
     * 
     * Setter for agent_nominee
     * @param Reale_Model_Users_Nominee $nominee
     * @return Reale_Model_Users_Agent
     */
    public function setNominee (Reale_Model_Users_Nominee $nominee)
    {
        $this->_nominee = $nominee;
        return $this;
    }
    /**
     * 
     * Getter for agent_nominee
     * @return Reale_Model_Users_Nominee
     */
    public function getNominee ()
    {
        return $this->_nominee;
    }
    /**
     * 
     * Setter for password 
     * @param string $password
     * @return Reale_Model_Users_Agent
     */
    public function setPassword ($password)
    {
        $this->_password = $password;
        return $this;
    }
    /**
     * 
     * Getter for password 
     * @return string $_password
     */
    public function getPassword ()
    {
        return $this->_password;
    }
    /**
     * 
     * Setter function for agent mapper 
     * @param Reale_Model_Users_AgentMapper $mapper
     * @return Reale_Model_Users_Agent
     */
    public function setMapper ($mapper)
    {
        $this->_mapper = $mapper;
        return $this;
    }
    /**
     * 
     * Getter function for agent mapper
     * @return Reale_Model_Users_AgentMapper $_mapper
     */
    public function getMapper ()
    {
        if (null === $this->_mapper) {
            $this->setMapper(new Reale_Model_Users_AgentMapper());
        }
        return $this->_mapper;
    }
    /**
     * 
     * To save new or update existing agent's informtion.
     * return Reale_Model_Users_Agent | null
     * 
     */
    public function save ()
    {
        $return = $this->getMapper()->save($this);
        if ($return) {
            if ($return instanceof Reale_Model_Users_Agent) {
                $this->setAgent_id($return->getAgent_id());
            }
            return true;
        }
        return null;
    }
    public function getInfo ()
    {
        $sql = 'SELECT
    `agent`.`agent_name`
    , `agent`.`address`
    , `agent`.`mobile_no`
    , `agent`.`landline_no`
    , `agent`.`occupation`
    , `agent`.`dob`
FROM
    `thesurya_reale`.`auth_user`
    INNER JOIN `agent` 
        ON (`auth_user`.`user_id` = `agent`.`username`)
WHERE (`auth_user`.`user_id` = ?)';
        $bind = array($this->getUsername());
        $dbTable = new Reale_Model_DbTable_Agent();
        return $dbTable->getDefaultAdapter()
            ->query($sql, $bind)
            ->fetch();
    }
    public function canSponser ()
    {
        $MAX = 5;
        $sql = 'SELECT
                    COUNT(`sponser_id`) AS `sponser_count`
                FROM
                    `agent_network`
                WHERE (`sponser_id`  = ?)';
        $bind = array($this->getAgent_id());
        $dbTable = new Reale_Model_DbTable_Agent();
        $sponsered = $dbTable->getDefaultAdapter()
            ->query($sql, $bind)
            ->fetchColumn();
        if('AGNT-A-0' == $this->getAgent_id()){
            return $sponsered;
        }
        else if ($sponsered < $MAX){
            return $sponsered;
        } else  {
            throw new Zend_Exception($this->getAgent_id().' has already sponsred '.$sponsered.' agents.', Zend_Log::ERR);
        }
    }
    

    public function sponser (Reale_Model_Users_Agent $agent)
    {
        $canSponser = self::canSponser();
        if (isset($canSponser)) {
            $sql = 'INSERT INTO `agent_network`(`sponser_id`,`agent_id`) VALUES (?,?)';
            $bind = array($this->getAgent_id(), $agent->getAgent_id());
            $dbTable = new Reale_Model_DbTable_Agent();
            return $dbTable->getDefaultAdapter()->query($sql, $bind);
        }
    }
    public function isValid ()
    {
        if (isset($this->_valid)) {
            return $this->_valid;
        } else {
            $sql = 'SELECT `agent_name` FROM `agent` WHERE (`agent_id` = ?)';
            $bind = array($this->getAgent_id());
            $dbTable = new Reale_Model_DbTable_Agent();
            $this->_valid = $dbTable->getDefaultAdapter()
                ->query($sql, $bind)
                ->fetchColumn();
            if ($this->_valid) {
                return $this->_valid;
            } else {
                throw new Zend_Exception('The . "' . $this->getAgent_id() . '" is not a valid agent id.');
            } 
        }
    }
}

